import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:thal/backstage/toolkit/utilities/color_palette.dart';
import 'package:thal/backstage/toolkit/utilities/dimension_rules.dart';
import 'package:thal/backstage/toolkit/widgets/stage_whisper.dart';
import 'package:thal/backstage/toolkit/widgets/loading_indicator.dart';

/// 举报聚焦幕布 - 举报弹窗
/// 当用户需要举报不当内容时，展示举报选项的弹窗
///
/// 使用示例：
/// ```dart
/// // 显示举报弹窗
/// ReportAccusation.raise(
///   context,
///   targetId: 'user_123',
///   onReportSubmitted: (reason) {
///     print('Reported: $reason');
///     // 执行举报操作
///   },
/// );
/// ```
class ReportAccusation {
  /// 私有构造函数，防止实例化
  ReportAccusation._();

  /// 举报原因枚举
  static const List<ReportReasonItem> reportReasons = [
    ReportReasonItem(
      icon: Icons.remove_circle_outline,
      title: 'Inappropriate Content',
      description: 'Content that violates community guidelines',
      color: ColorPalette.red,
    ),
    ReportReasonItem(
      icon: Icons.warning_amber_rounded,
      title: 'Hate Speech',
      description: 'Offensive or discriminatory language',
      color: ColorPalette.pink,
    ),
    ReportReasonItem(
      icon: Icons.visibility_off_outlined,
      title: 'Disturbing Content',
      description: 'Content that may be disturbing or harmful',
      color: ColorPalette.pink,
    ),
    ReportReasonItem(
      icon: Icons.gavel_rounded,
      title: 'Illegal Activity',
      description: 'Content promoting illegal activities',
      color: ColorPalette.red,
    ),
    ReportReasonItem(
      icon: Icons.campaign_outlined,
      title: 'Spam/Advertisement',
      description: 'Unwanted promotional content',
      color: ColorPalette.blue2,
    ),
    ReportReasonItem(
      icon: Icons.error_outline,
      title: 'Other Issues',
      description: 'Other concerns not listed above',
      color: ColorPalette.blue1,
    ),
    ReportReasonItem(
      icon: Icons.block_rounded,
      title: 'Block User',
      description: 'Block this user from contacting you',
      color: ColorPalette.black,
    ),
  ];

  /// 升起控诉幕布 - 显示举报弹窗
  static void raise(
    BuildContext context, {
    required String targetId,
    String? targetType,
    Function(String reason)? onReportSubmitted,
  }) {
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      builder:
          (context) => _AccusationSheet(
            targetId: targetId,
            targetType: targetType,
            onReportSubmitted: onReportSubmitted,
          ),
    );
  }
}

/// 举报原因数据模型
class ReportReasonItem {
  final IconData icon;
  final String title;
  final String description;
  final Color color;

  const ReportReasonItem({
    required this.icon,
    required this.title,
    required this.description,
    required this.color,
  });
}

/// 控诉表单内部组件
class _AccusationSheet extends StatefulWidget {
  final String targetId;
  final String? targetType;
  final Function(String reason)? onReportSubmitted;

  const _AccusationSheet({
    required this.targetId,
    this.targetType,
    this.onReportSubmitted,
  });

  @override
  State<_AccusationSheet> createState() => _AccusationSheetState();
}

class _AccusationSheetState extends State<_AccusationSheet> {
  String? _selectedReason;

  /// 提交举报
  void _submitReport() {
    if (_selectedReason == null) {
      StageWhisper.caution('Please select a reason');
      return;
    }

    LoadingCurtain.raiseCurtain(message: 'Submitting report...');

    // 模拟提交过程
    Future.delayed(const Duration(seconds: 1), () {
      LoadingCurtain.lowerCurtain();
      Navigator.of(context).pop();
      StageWhisper.cheer('Report submitted successfully');

      // 执行回调
      widget.onReportSubmitted?.call(_selectedReason!);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: ColorPalette.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(24.r),
          topRight: Radius.circular(24.r),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 标题区域
          Container(
            width: double.infinity,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
                colors: [
                  ColorPalette.red.withValues(alpha: 0.1),
                  ColorPalette.pink.withValues(alpha: 0.1),
                ],
              ),
            ),
            child: Column(
              children: [
                Text('Report Content')
                    .fontSize(22.sp)
                    .textColor(ColorPalette.black)
                    .arimo(weight: FontWeight.bold),
                4.h.heightBox,
                Text('Help us maintain a safe community')
                    .fontSize(12.sp)
                    .textColor(ColorPalette.black.withValues(alpha: 0.6))
                    .arimo(),
              ],
            ).wrapHorizontal(24.w).wrapVertical(16.h),
          ),

          // 举报原因列表
          Column(
            children:
                ReportAccusation.reportReasons
                    .map((reason) => _buildReasonItem(reason))
                    .toList(),
          ).wrapHorizontal(16.w).wrapTop(8.h),

          // 底部按钮区域
          Container(
            decoration: BoxDecoration(
              color: ColorPalette.white,
              border: Border(
                top: BorderSide(
                  color: ColorPalette.grey.withValues(alpha: 0.5),
                  width: 1,
                ),
              ),
            ),
            child: Column(
              children: [
                // 提交按钮
                SizedBox(
                  width: double.infinity,
                  child: ElevatedButton(
                    onPressed: _selectedReason != null ? _submitReport : null,
                    style: ElevatedButton.styleFrom(
                      backgroundColor: ColorPalette.red,
                      foregroundColor: ColorPalette.white,
                      disabledBackgroundColor: ColorPalette.grey,
                      disabledForegroundColor: ColorPalette.black.withValues(
                        alpha: 0.4,
                      ),
                      padding: EdgeInsets.symmetric(vertical: 14.h),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12.r),
                      ),
                      elevation: 0,
                    ),
                    child: Text(
                      'Submit Report',
                    ).fontSize(16.sp).arimo(weight: FontWeight.w600),
                  ),
                ),
                12.h.heightBox,
                // 取消按钮
                SizedBox(
                  width: double.infinity,
                  child: TextButton(
                    onPressed: () => Navigator.of(context).pop(),
                    style: TextButton.styleFrom(
                      foregroundColor: ColorPalette.black.withValues(
                        alpha: 0.6,
                      ),
                      padding: EdgeInsets.symmetric(vertical: 14.h),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(12.r),
                      ),
                    ),
                    child: Text(
                      'Cancel',
                    ).fontSize(14.sp).arimo(weight: FontWeight.w500),
                  ),
                ),
              ],
            ).wrapAll(16.w),
          ),
        ],
      ),
    );
  }

  /// 构建举报原因项
  Widget _buildReasonItem(ReportReasonItem reason) {
    final isSelected = _selectedReason == reason.title;

    return Container(
      margin: EdgeInsets.only(bottom: 8.h),
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 10.h),
      decoration: BoxDecoration(
        color:
            isSelected
                ? reason.color.withValues(alpha: 0.1)
                : ColorPalette.white,
        border: Border.all(
          color:
              isSelected
                  ? reason.color
                  : ColorPalette.grey.withValues(alpha: 0.5),
          width: isSelected ? 2 : 2,
        ),
        borderRadius: BorderRadius.circular(10.r),
      ),
      child: Row(
        children: [
          // 图标
          Container(
            width: 36.w,
            height: 36.h,
            decoration: BoxDecoration(
              color: reason.color.withValues(alpha: 0.1),
              borderRadius: BorderRadius.circular(8.r),
            ),
            child: Icon(reason.icon, color: reason.color, size: 20.sp),
          ),
          12.w.widthBox,
          // 文字内容
          Expanded(
            child: Text(reason.title)
                .fontSize(14.sp)
                .textColor(ColorPalette.black)
                .arimo(weight: FontWeight.w600),
          ),
          // 选中指示器
          if (isSelected)
            Icon(Icons.check_circle, color: reason.color, size: 20.sp)
          else
            Icon(Icons.circle_outlined, color: ColorPalette.grey, size: 20.sp),
        ],
      ),
    ).withTapEffect(
      onTap: () {
        setState(() {
          _selectedReason = reason.title;
        });
      },
    );
  }
}
